package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.spi.RegisterableService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {


    @Resource
    private ServeMapper serveMapper;

    @Resource
    private IRegionService regionService;




    /**
     * 首页列表
     * @param regionId
     * @return
     */
    @Override
    @Caching(cacheable = {
          //result为空时，属于缓存穿透情况 缓存时间是30分钟
        @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,
                key = "#regionId",unless = "#result.size()!=0",cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
            //result不为空永久缓存
        @Cacheable(value = RedisConstants.CacheName.SERVE_ICON,
                key = "#regionId",unless = "#result.size()==0",cacheManager = RedisConstants.CacheManager.FOREVER)
    })
    public List<ServeCategoryResDTO> serveCacheList(Long regionId) {
        //查询该城市是否启用
        Region region = regionService.getById(regionId);
        if (ObjectUtil.isEmpty(region) || ObjectUtil.equal(FoundationStatusEnum.DISABLE.getStatus(), region.getActiveStatus())) {
            return Collections.emptyList();
        }
        //查询该城市编码下的图标
        List<ServeCategoryResDTO> list = serveMapper.findServiceCateById(regionId);
        if (ObjectUtil.isEmpty(list)) {
            return Collections.emptyList();
        }



        int index=  list.size()>=2?2: list.size();

        List<ServeCategoryResDTO> serveCategoryResDTOS =
                new ArrayList<>(list.subList(0,index));

        serveCategoryResDTOS.stream().forEach(item->{


            List<ServeSimpleResDTO> serveResDTOList = item.getServeResDTOList();


            int index2 = serveResDTOList.size()>=4?4:serveResDTOList.size();


            List<ServeSimpleResDTO> serveCategoryResDTOList = new ArrayList<>(serveResDTOList.subList(0,index2));

            item.setServeResDTOList(serveCategoryResDTOList);
        });
        return serveCategoryResDTOS;
    }

    @Override
    public List<ServeAggregationTypeSimpleResDTO> queryServeTypeListByRegionIdCache(Long regionId) {


        //首先查询该城市是否是启用状态
        Region regionServiceById = regionService.getById(regionId);

        if(ObjectUtil.equal(FoundationStatusEnum.DISABLE.getStatus(),regionServiceById.getActiveStatus())){
            return Collections.emptyList();
        }

        List<ServeAggregationTypeSimpleResDTO> list = serveMapper.findServeTypeListByRegionId(regionId);
        if(ObjectUtil.isEmpty(list)){
            return Collections.emptyList();
        }

        return list;
    }






}
